package com.cloud.logger.zchain.spring.config;

import com.cloud.logger.zchain.adapter.http.AdapterHttpHeaders;
import com.cloud.logger.zchain.core.ChainNode;
import com.cloud.logger.zchain.core.common.threadlocal.LoggerThreadLocal;
import com.cloud.logger.zchain.zchainclient.interceptor.ZchainFilter;
import com.cloud.logger.zchain.zchainclient.listener.AngelaInit;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * User: wcy
 * Date: 2017/11/27
 * Time: 15:27
 */
@Configuration
@Order(2)
public class ZchainClientConfiguration {

    @Bean
    public ZchainFilter getLogger(){
        return new ZchainFilter();
    }

    @Bean
    public RequestInterceptor headerInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
                        .getRequestAttributes();
                requestTemplate.header(AdapterHttpHeaders.RootId.getName(), LoggerThreadLocal.getLoggerId());
                requestTemplate.header(AdapterHttpHeaders.NodeId.getName(), LoggerThreadLocal.getNodeLoggerId());
                requestTemplate.header(AdapterHttpHeaders.ParentNodeId.getName(), LoggerThreadLocal.getNodeLoggerId());
            }
        };
    }

}
